package com.excilys.gwt.first.application.client;

import com.excilys.gwt.first.application.shared.AuthenticationService;
import com.excilys.gwt.first.application.shared.AuthenticationServiceAsync;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratedTabPanel;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class ui implements EntryPoint {

	private VerticalPanel container;

	private DecoratedTabPanel tabPanel;
	private Panel loginForm;
	private Panel registrationForm;

	/** Main */
	@SuppressWarnings("deprecation")
	@Override
	public void onModuleLoad() {
		// Récupère un panel centré de taille 800
		container = createContainer();
		loginForm = createLoginForm();
		registrationForm = createRegistrationForm();
		tabPanel = createTabPanel();
		tabPanel.add(loginForm, "Connexion");
		tabPanel.add(registrationForm, "Inscription");
		tabPanel.selectTab(0);
		container.add(tabPanel);
	}

	@SuppressWarnings("deprecation")
	private DecoratedTabPanel createTabPanel() {
		DecoratedTabPanel tabPanel = new DecoratedTabPanel();
		tabPanel.setWidth("100%");
		tabPanel.setAnimationEnabled(true);
		return tabPanel;
	}

	private final AuthenticationServiceAsync authService = GWT.create(AuthenticationService.class);

	final TextBox loginBox = new TextBox();
	final TextBox passBox = new TextBox();

	final Label errorMessage = new Label();

	/**
	 * Crée un FormPanel de login
	 * @return le FormPanel
	 */
	private VerticalPanel createLoginForm() {
		VerticalPanel contain = new VerticalPanel();
		contain.setWidth("100%");
		contain.setHeight("100%");
		FormPanel form = new FormPanel();
		form.setStylePrimaryName("container");
		form.setWidth("100%");
		VerticalPanel holder = new VerticalPanel();
		holder.setSpacing(5);
		holder.setWidth("100%");

		errorMessage.setStyleName("error");
		errorMessage.setWidth("100%");
		errorMessage.setVisible(false);

		// holder.add(errorMessage);
		holder.add(new Label("Identifiant"));
		holder.add(loginBox);
		holder.add(new Label("Mot de passe"));
		holder.add(passBox);
		Button sendButton = new Button("Se connecter");
		holder.add(sendButton);

		final AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
			@Override
			public void onSuccess(Boolean success) {
				if (success) {
					GWT.log("LOGIN SUCCESS");
				} else {
					errorMessage.setText("Identifiants incorrects");
					errorMessage.setVisible(true);
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				errorMessage.setText("Problème serveur");
				errorMessage.setVisible(true);
			}
		};

		sendButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				String login = loginBox.getText();
				String pass = passBox.getText();
				authService.authenticate(login, pass, callback);
			}
		});

		form.add(holder);

		contain.add(errorMessage);
		contain.add(form);
		return contain;
	}

	/**
	 * Crée un panel pour l'enregistrement
	 * @return un Panel vide pour le moment
	 */
	private HorizontalPanel createRegistrationForm() {
		HorizontalPanel panel = new HorizontalPanel();
		panel.setStylePrimaryName("container");
		panel.add(new Label("Non implémenté"));
		return panel;
	}

	/**
	 * Retourne un panel centré sur la page
	 * @param i Largeur du panel
	 * @return un panel centré sur la page
	 */
	private VerticalPanel createContainer() {

		RootPanel root = RootPanel.get();
		root.setStyleName("rootPanel");

		HorizontalPanel container = new HorizontalPanel();
		container.setWidth("100%");

		VerticalPanel central = new VerticalPanel();
		central.add(spacer(1, 30));
		central.setWidth("400px");
		central.setHeight("100%");

		container.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		container.add(central);
		root.add(container);
		return central;
	}

	private Panel spacer(int width, int height) {
		Panel pan = new SimplePanel();
		pan.setHeight(height + "px");
		pan.setWidth(width + "px");
		return pan;
	}
}
